<template>
<div>
  <el-row>
    <el-col :span="24"><div class="grid-content bg-purple-light">
      <div style="float: left; margin: 20px 100px; font-weight: 700;color:#606266;font-size: 20px"><i class="el-icon-search"></i>权限查询</div>
    </div></el-col>
  </el-row>
  <el-form :inline="true"  style="text-align: left; margin: 0 10%">
    <el-form-item>
      <el-button type="info" @click="add()" plain>新增</el-button>
    </el-form-item>
  </el-form>
  <el-row>
    <el-col :span="24"><div class="grid-content bg-purple-light">
      <div style="float: left; margin: 0 100px; font-weight: 700;color:#606266;font-size: 20px"><i class="el-icon-s-unfold"></i>数据列表</div>
    </div></el-col>
  </el-row>
  <div id="s" class="animate__animated animate__zoomIn animate__delay-1s" style="margin-top: 20px">
  <el-table
    :data="tableData"
    style="width: 100%;" :border="true"
    row-key="id"
    border
    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    :header-cell-style="{background:'#209e91', color:'#ffffff',textAlign:'center'}">
    <el-table-column
      prop="name"
      label="权限名称"
      width="250"
      align="center">
    </el-table-column>
    <el-table-column
      prop="url"
      label="权限URL"
      width="250"
      align="center">
    </el-table-column>
    <el-table-column
      prop="seq"
      label="排序"
      align="center">
    </el-table-column>
    <el-table-column
      prop="mark"
      label="备注"
      align="center">
    </el-table-column>
    <el-table-column
      prop="createId"
      label="新增人员"
      align="center">
    </el-table-column>
    <el-table-column
      prop="createTime"
      label="新增时间"
      align="center">
    </el-table-column>
    <el-table-column
      prop="modifyId"
      label="修改人员"
      align="center">
    </el-table-column>
    <el-table-column
      prop="modifyTime"
      label="修改时间"
      align="center">
    </el-table-column>
    <el-table-column
      label="操作"
      width="250"
      align="center">
      <template v-slot="scope">
        <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
        <el-popconfirm title="确定删除吗？" @confirm="remove(scope.row)">
          <el-button slot="reference" type="text" size="small">删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
    <el-row>
      <el-col :span="24"><div class="grid-content bg-purple-light">
        <div style="margin: 20px"></div>
      </div></el-col>
    </el-row>
  </div>
  <!--  新增/修改页面-->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="30%">
      <span>
        <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
           <el-form-item label="上级权限" prop="pid">
              <el-cascader v-model="form.pid"
                :options="sysAuthListOptions"
                :disabled="disabled"
                :props="{ checkStrictly: true,value:'id',label:'name',emitPath:false }"
                clearable style="width: 100%"></el-cascader>
          </el-form-item>
          <el-form-item label="权限名称" prop="name">
             <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="图标" prop="icon">
             <el-input v-model="form.icon"></el-input>
          </el-form-item>
           <el-form-item label="权限URL" prop="url">
             <el-input v-model="form.url"></el-input>
          </el-form-item>
           <el-form-item label="权限排序" prop="seq">
             <el-input-number v-model="form.seq" :min="1" :max="100" style="width: 100%"></el-input-number>
          </el-form-item>
           <el-form-item label="权限备注" prop="mark">
             <el-input v-model="form.mark"></el-input>
          </el-form-item>
        </el-form>
      </span>
    <span slot="footer" class="dialog-footer">
        <el-button @click="show = false;disabled=false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "SysAuth",
  data(){
    return {
      tableData: [],
      sysAuthListOptions:[],
      //表单对象
      form: {},
      //显示新增、修改
      show: false,
      //标题
      title: '',
      disabled: false,
      //校验对象
      rules: {
        name: [
          {required: true, message: '请输入权限名称', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '请输入权限URL', trigger: 'blur'}
        ],
        seq: [
          {required: true, message: '请输入排序数字', trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    //获取全部权限列表
    getList(){
      this.$axios({
        url: 'sys/auth/list',
        method: 'post'
      }).then(res => {
        if (res.data.success) {
          this.tableData = res.data.object;
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    //获取全部父权限（下拉菜单用）
    getParents(){
      this.$axios({
        url: 'sys/auth/getParents',
        method: 'post'
      }).then(res => {
        if (res.data.success) {
          this.sysAuthListOptions = res.data.object;
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    add(){
      //设置标题
      this.title = '新增'
      //显示对话框
      this.show = true
      //清空表单
      this.form = {}
      //清空校验结果
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate();
      })
    },
    edit(row) {
      this.title = '修改'
      //清空表单
      this.form = {}
      //给form对象赋值
      this.form = Object.assign({}, row);
      //显示对话框
      this.show = true;
      console.info(row)
      if(row.pid==null){
         this.disabled = true
      }
      //清空校验结果
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate();
      })
    },
    remove(row) {
      this.$axios({
        url: 'sys/auth/remove/' + row.id,
        method: 'post'
      }).then(res => {
        if (res.data.success) {
          this.$message({
            message: res.data.message,
            type: 'success'
          });
          this.getList();
        } else {
          this.$message.error(res.data.message);
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          //校验通过
          if (this.title == '新增') {
            this.$refs['ruleForm'].validate((valid) => {
              if (valid) {
                this.$axios({
                  url: 'sys/auth/add',
                  method: 'post',
                  data: this.form
                }).then(res => {
                  if (res.data.success) {
                    this.show = false;
                    this.disabled=false;
                    this.getList();
                    this.getParents();
                  } else {
                    this.$message.error(res.data.message);
                  }
                }).catch(err => {
                  this.$message.error(err.message);
                })
              }
            });
          } else {
            this.$axios({
              url: 'sys/auth/edit',
              method: 'post',
              data: this.form
            }).then(res => {
              if (res.data.success) {
                this.show = false;
                this.getList();
                this.getParents();
              } else {
                this.$message.error(res.data.message);
              }
            }).catch(err => {
              this.$message.error(err.message);
            })
          }
        }
      });
    }
  },
  created() {
    this.getList()
    this.getParents()
  }
}
</script>

<style scoped>
.el-button--info:hover {
  background: #209e91;
  border-color: #209e91;
  color: #FFF;
}
#s{
  width:90%;
  margin:10px auto;
  background-color: #ffffff;
  box-shadow: 10px 10px 5px #c4c6c7;
  border-radius: 5px
}
</style>
